package com.scau402.system1.domain.po;

import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.scau402.common.core.domain.model.LoginUser;
import com.scau402.system1.domain.vo.WmsAdjustJadeDetailVO;
import com.scau402.system1.domain.dto.WmsCreateAdjustBoundOrderDTO;
import com.scau402.system1.utils.IdGenerator;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 库存调整单
 * @since 2024/2/1
 */
@Data
@TableName("wms_adjustbound_order")
@NoArgsConstructor
@AllArgsConstructor
public class WmsAdjustBoundOrderPO {
    /**
     * 主键
     */
    private Long id;
    /**
     * 调整单号
     */
    private String orderCode;
    /**
     * 盘点单id
     */
    private Long checkOrderId;
    /**
     * 原库存数
     */
    private Long oriNum;
    /**
     * 修改后库存数
     */
    private Long newNum;
    /**
     * 盘亏数
     */
    private Long deficitNum;
    /**
     * 盘盈数
     */
    private Long profitNum;
    /**
     * 备注
     */
    private String remark;
    /**
     * 调整人员
     */
    private String createBy;
    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createTime;
    /**
     * 更新时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updateTime;
    /**
     * 调整人员id
     */
    private Long createById;
    /**
     * 租户id
     */
    private Long tenantId;

    /**
     * 参数设置
     * @param user 用户
     * @param idGenerator id生成器
     * @param jadeList jade列表
     * @param createOrder 所需参数
     */
    public WmsAdjustBoundOrderPO(LoginUser user, IdGenerator idGenerator, List<WmsAdjustJadeDetailVO> jadeList, WmsCreateAdjustBoundOrderDTO createOrder){
        this.orderCode = "KT"+System.currentTimeMillis();
        this.id = idGenerator.nextId("adjustbound");
        this.checkOrderId = createOrder.getCheckOrderId();
        // 统计
        long oriNum = 0L,newNum = 0L,deficitNum = 0L,profitNum = 0L;
        for (WmsAdjustJadeDetailVO wmsAdjustJadeDetailVO : jadeList) {
            oriNum += wmsAdjustJadeDetailVO.getOriNum();
            newNum += wmsAdjustJadeDetailVO.getNewNum();
            if (wmsAdjustJadeDetailVO.getOriNum() < wmsAdjustJadeDetailVO.getNewNum())
                profitNum += (wmsAdjustJadeDetailVO.getNewNum() - wmsAdjustJadeDetailVO.getOriNum());
            else deficitNum -= (wmsAdjustJadeDetailVO.getNewNum() - wmsAdjustJadeDetailVO.getOriNum());
        }
        this.oriNum = oriNum;
        this.newNum = newNum;
        this.deficitNum = deficitNum;
        this.profitNum = profitNum;
        this.createBy = user.getUsername();
        this.createById = user.getUserId();
        this.remark = createOrder.getRemark();
        this.createTime = LocalDateTime.now();
        this.updateTime = LocalDateTime.now();
        this.tenantId = user.getTenantId();
    }
}
